﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Web;

namespace ExtendedWorkFlow
{

    public class ParameterCollection : CollectionBase
    {

        #region Public Functions

        /// <summary>
        /// Add KeyValuePair to the collection
        /// </summary>
        /// <param name="Value">Value to add</param>
        /// <returns>Index of the KeyValuePair in the collection</returns>
        public int Add(KeyValuePair<string, string> Value)
        {
            int intIndex = this.IndexOf(Value.Key);
            if (intIndex == -1)
                return this.List.Add(Value);
            else
                this.List[intIndex] = Value;

            return intIndex;
        }

        /// <summary>
        /// Add KeyValuePair to the collection
        /// </summary>
        /// <param name="Key">Name of the key</param>
        /// <param name="Value">Value of the key</param>
        /// <returns>Index of the KeyValuePair in the collection</returns>
        public int Add(string Key, string Value)
        {
            return this.Add(new KeyValuePair<string, string>(
                Key,
                Value));
        }

        /// <summary>
        /// Remove item from the collection
        /// </summary>
        /// <param name="Value">Value to remove</param>
        public void Remove(KeyValuePair<string, string> Value)
        {
            if (this.List.Contains(Value))
                this.List.Remove(Value);
        }

        /// <summary>
        /// Find the index of a key within the collection
        /// </summary>
        /// <param name="Key">Key to find</param>
        /// <returns>Index of key, -1 if not found</returns>
        public int IndexOf(string Key)
        {
            foreach (KeyValuePair<string, string> objValue in this.List)
            {
                if (string.Compare(objValue.Key , Key, true) == 0)
                    return List.IndexOf(objValue);
            }

            return -1;
        }

        /// <summary>
        /// Find the index of a KeyValuePair within the collection
        /// </summary>
        /// <param name="Key">Key to find</param>
        /// <returns>Index of key, -1 if not found</returns>
        public int IndexOf(KeyValuePair<string, string> Value)
        {
            foreach (KeyValuePair<string, string> objValue in this.List)
            {
                if (string.Compare(objValue.Key, Value.Key, true) == 0)
                    return List.IndexOf(objValue);
            }

            return -1;
        }

        #endregion

        #region Properties

        /// <summary>
        /// KeyValuePair collection base
        /// </summary>
        /// <param name="Key">Key to get or set</param>
        /// <returns>KeyValuePair within the collection</returns>
        public KeyValuePair<string, string> this [string Key]
        {
            get
            {
                int intIndex = this.IndexOf(Key);
                if (intIndex != -1)
                    return (KeyValuePair<string, string>)this.List[intIndex];
                else
                    return new KeyValuePair<string, string>(string.Empty, string.Empty);
            }
            set
            {
                int intIndex = this.IndexOf(Key);
                if (intIndex != -1)
                    this.List[intIndex] = value;
            }
        }

        /// <summary>
        /// KeyValuePair collection base
        /// </summary>
        /// <param name="Index">Index of the key to get or set</param>
        /// <returns>KeyValuePair within the collection</returns>
        public KeyValuePair<string, string> this[int Index]
        {
            get
            {
                if (Index >= 0 && Index < this.List.Count)
                    return (KeyValuePair<string, string>)this.List[Index];
                else
                    return new KeyValuePair<string, string>(string.Empty, string.Empty);
            }
            set
            {
                if (Index >= 0 && Index < this.List.Count)
                    this.List[Index] = value;
            }
        }

        #endregion

    }

}